package cn.edu.neusoft.yuejian4.yuejian_4.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.GameListActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.LoginActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.MasterListActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.ActivityListAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Activity;

import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.HBanner;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.List_TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.ActivityModel;
import cn.edu.neusoft.yuejian4.yuejian_4.model.BannerModel;


/**
 * Created by Lix on 2018/10/9.
 */

public class HomeFragment extends Fragment implements View.OnClickListener {

    private View view;
    private ImageView home_master_img,home_game_img;
    private RecyclerView recyclerView;
    private Banner banner;
  //  private ArrayList<String> list_path;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home,container,false);
        initViews();
        initEvent();
        ActivityModel activityModel = new ActivityModel();
        activityModel.allActivity(listener);
        BannerModel bannerModel = new BannerModel();
        bannerModel.getBanner(bannerListenrt);
        return view;
    }

    List_TListener<HBanner> bannerListenrt = new List_TListener<HBanner>() {
        @Override
        public void onResponse(List<HBanner> list) {

            HBanner hBanner;
            ArrayList<String> list_pic = new ArrayList<String>();
            for (int i = 0;i< list.size();i++){
                hBanner = list.get(i);
               list_pic.add(Server.PIC_URL_J+hBanner.getPic()) ;
            }
          initbanner(list_pic);
        }

        @Override
        public void onFail(String msg) {

        }

    };



    List_TListener<Activity>  listener =  new List_TListener<Activity>() {
        @Override
        public void onResponse(List<Activity> list) {
            ActivityListAdapter activityListAdapter = new ActivityListAdapter(getContext(),list);
            recyclerView.setAdapter(activityListAdapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initEvent() {
        home_game_img.setOnClickListener(this);
        home_master_img.setOnClickListener(this);

    }

    private void initViews() {
        recyclerView = (RecyclerView)view.findViewById(R.id.rv_activity);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        home_game_img = (ImageView) view.findViewById(R.id.home_game_img);
        home_master_img = (ImageView) view.findViewById(R.id.home_master_img);
        banner = (Banner)view.findViewById(R.id.banner);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.home_game_img:
                Intent intent = new Intent(getContext(),GameListActivity.class);
                startActivity(intent);
                break;
            case R.id.home_master_img:
                Intent intent1 = new Intent(getContext(),MasterListActivity.class);
                startActivity(intent1);
                break;
        }
    }

    public void initbanner(List list_path){
//        //放图片地址的集合
//        list_path = new ArrayList<>();
//        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg");
//        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
//        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
//        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg");

        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器，图片加载器在下方
        banner.setImageLoader(new MyLoader());
        //设置图片网址或地址的集合
        banner.setImages(list_path);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        //设置轮播图的标题集合
       // banner.setBannerTitles(list_title);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    //轮播图的监听方法
                    public void OnBannerClick(int position) {
                        Log.i("tag", "你点了第"+position+"张轮播图");
                    }
                })
                //必须最后调用的方法，启动轮播图。
                .start();
    }
    //自定义的图片加载器
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load((String) path).into(imageView);
        }
    }
}
